package httpHandler

import (
	"encoding/json"
	baseHttp "gitee.com/zaiqiang231/go-base-app/base_app/http"
	sessionHandler "gitee.com/zaiqiang231/go-base-session/app/http/handler"
	"gitee.com/zaiqiang231/zcamp-service-camp/app/http/service"
	"gitee.com/zaiqiang231/zcamp-service-camp/app/model/req"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"net/http"
	"reflect"
	"time"
)

func GetCampList(c *gin.Context) {
	var request req.CampListReq
	if err := c.ShouldBindJSON(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	list, total, err := service.ServiceCampInstance.GetList(request)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(c, 0, "success", gin.H{
		"list":     list,
		"total":    total,
		"page":     request.Page,
		"pageSize": request.PageSize,
	})
}

func CreateCamp(c *gin.Context) {
	uid := c.GetInt64(KEY_AUTH_UID)
	var request req.CampCreateReq
	if err := c.ShouldBindJSON(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	request.Uid = uid
	request.PublishTime = time.Now()

	result, tmperr := json.Marshal(request.ReqImages)
	if tmperr != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, tmperr.Error(), nil)
		return
	}
	request.Pictures = string(result)

	var conditionList = make([]int64, 0)
	updateConditionList(&conditionList, request.ReqConditions)
	result, tmperr = json.Marshal(conditionList)
	if tmperr != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, tmperr.Error(), nil)
		return
	}
	request.Conditions = string(result)

	err := service.ServiceCampInstance.Create(&request, conditionList)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(c, 0, "success", nil)
}

func UpdateCamp(c *gin.Context) {

	//var request req.CampUpdateReq
	//if err := c.ShouldBindBodyWith(&request, binding.JSON); err != nil {
	//	baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
	//	return
	//}

	var target map[string]interface{}
	if err := c.ShouldBindBodyWith(&target, binding.JSON); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}

	result, tmperr := json.Marshal(target["req_images"])
	if tmperr != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, tmperr.Error(), nil)
		return
	}
	target["pictures"] = string(result)

	result, tmperr = json.Marshal(target["req_conditions"])
	if tmperr != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, tmperr.Error(), nil)
		return
	}
	var reqConditions []req.ConditionReq
	tmperr = json.Unmarshal(result, &reqConditions)
	if tmperr != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, tmperr.Error(), nil)
		return
	}
	var conditionList = make([]int64, 0)
	updateConditionList(&conditionList, reqConditions)
	result, tmperr = json.Marshal(conditionList)
	if tmperr != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, tmperr.Error(), nil)
		return
	}
	target["conditions"] = string(result)

	var request req.CampUpdateReq
	elem := reflect.TypeOf(&request).Elem() //通过反射获取type定义
	for i := 0; i < elem.NumField(); i++ {
		if elem.Field(i).Tag.Get("gorm") == "-" && len(elem.Field(i).Tag.Get("json")) > 0 {
			delete(target, elem.Field(i).Tag.Get("json"))
		}
	}

	err := service.ServiceCampInstance.UpdateForMap(target, conditionList)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(c, 0, "success", nil)
}

func updateConditionList(resultList *[]int64, treeNodes []req.ConditionReq) {
	length := len(treeNodes)
	if length > 0 {
		for i := 0; i < length; i++ {
			*resultList = append(*resultList, treeNodes[i].ConditionId)
			if len(treeNodes[i].Children) > 0 {
				updateConditionList(resultList, treeNodes[i].Children)
			}
		}
	}
}

func DeleteCampList(c *gin.Context) {
	var request req.IdsReq
	if err := c.ShouldBindJSON(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	err := service.ServiceCampInstance.DeleteList(request)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(c, 0, "success", nil)
}

func CampListSearch(c *gin.Context) {
	var request req.CampListSearchReq
	if err := c.ShouldBindJSON(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	atom := sessionHandler.GetAtom(c)
	list, total, err := service.ServiceCampInstance.SearchList(c, atom, request)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(c, 0, "success", gin.H{
		"list":     list,
		"total":    total,
		"page":     request.Page,
		"pageSize": request.PageSize,
	})
}

func CampListRank(c *gin.Context) {
	var request req.CampListRankReq
	if err := c.ShouldBindJSON(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	list, total, err := service.ServiceCampInstance.RankList(request)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(c, 0, "success", gin.H{
		"list":     list,
		"total":    total,
		"page":     request.Page,
		"pageSize": request.PageSize,
	})
}

func CampNationNum(c *gin.Context) {
	var request req.CampNationNumReq
	if err := c.ShouldBindJSON(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	list, err := service.ServiceCampInstance.CampNationNum(request)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(c, 0, "success", list)
}

func CampDetail(c *gin.Context) {
	var request req.CampDetailReq
	if err := c.ShouldBindQuery(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	atom := sessionHandler.GetAtom(c)
	data, err := service.ServiceCampInstance.CampDetail(c, atom, request.CampId)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(c, 0, "success", data)
}

func AddCollect(c *gin.Context) {
	var request req.CollectReq
	if err := c.ShouldBindJSON(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	uid := c.GetInt64(KEY_AUTH_UID)
	err := service.ServiceCampInstance.SetCollectInfo(c, request, uid)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	baseHttp.WriteResponse(c, 0, "success", nil)
}

func GreenAgreeStatus(c *gin.Context) {
	atom := sessionHandler.GetAtom(c)
	if atom.Uid > 0 {
		status := service.ServiceCampInstance.GetGreenStatusInfo(atom.Uid)
		baseHttp.WriteResponse(c, 0, "success", gin.H{
			"status": status,
		})
	} else {
		baseHttp.WriteResponse(c, 0, "success", gin.H{
			"status": 0,
		})
	}
}
